package one.four.fifteen;

import java.util.*;

public class TwoSumFaster {

	public static int fac(int n) {
		int r = 1;
		for (int i = n; i > 2; i--) {
			r *= i;
		}
		return r;
	}

	public static int c(int n, int r) {
		int res = fac(n) / (fac(n - r) * fac(r));
		return res;
	}

	public static int count(int[] a) {
		int r = 0;
		int z = 0;
		Map<Integer, Integer> m = new HashMap<Integer, Integer>();
		// Map<Integer, Integer> ori = new HashMap<Integer, Integer>();
		int tmp;
		for (int i = 0; i < a.length; i++) {
			if (a[i] == 0) {
				z++;
			}

			if (a[i] > 0) {
				if (m.containsKey(a[i])) {
					tmp = m.get(a[i]);
					m.put(a[i], tmp + 1);
				} else {
					m.put(a[i], 1);
				}
			}
		}
		int ori = 0;
		for (int i : m.values()) {
			ori += i;
		}
		if (z >= 2) {
			r += c(z, 2);
		}
		for (int i = 0; i < a.length; i++) {
			if (a[i] < 0) {
				if (m.containsKey(-a[i])) {
					tmp = m.get(-a[i]);
					m.put(-a[i], tmp - 1);
				}
			}
		}
		int aft = 0;
		for (int i : m.values()) {
			aft += i;
		}
		r += (ori - aft);
		return r;
	}

	public static void main(String[] args) {
		// System.out.println(c(3, 2));
		int[] a = { 1, 1, 2, 3, -1, -2, -3, 0, 0 };
		int r = count(a);
		System.out.println(r);

	}

}
